/*!
 * jQuery Plugin boxes v0.3
 *
 * by zzbaivong
 * http://devs.forumvi.com/
 */
(function(g) {
    var p = {
        init: function(b) {
            var a = g.extend({
                mode: "",
                title: "",
                message: "",
                okBtn: "X\u00e1c nh\u1eadn",
                cancelBtn: "H\u1ee7y b\u1ecf",
                closeBtn: "\u0110\u00f3ng",
                inputTxt: "",
                width: "auto",
                height: "auto",
                minWidth: 250,
                minHeight: 120,
                maxWidth: 800,
                maxHeight: 600,
                padding: [25, 20],
                border: [3, 3],
                autoClose: 0,
                noClose: !1,
                clickOut: !1,
                messString: !0,
                autoResize: !0,
                okBtnShow: !1,
                cancelBtnShow: !1,
                closeBtnShow: !1,
                inputTxtShow: !1,
                ok: function() {},
                cancel: function() {},
                close: function() {},
                input: function() {},
                button: function() {},
                helper: function() {},
                temp: {}
            }, b),
                h = {
                    boxes: '<div class="zzBoxes"></div>',
                    overlay: '<div class="zzBoxes_overlay"></div>',
                    inner: '<div class="zzBoxes_inner"></div>',
                    title: '<h2 class="zzBoxes_title"></h2>',
                    close: '<div class="zzBoxes_close"></div>',
                    content: '<div class="zzBoxes_content"></div>',
                    mess: '<div class="zzBoxes_mess"></div>',
                    input: '<input class="zzBoxes_input" type="text" />',
                    button: '<div class="zzBoxes_button"></div>',
                    ok: '<div class="zzBoxes_ok"></div>',
                    cancel: '<div class="zzBoxes_cancel"></div>'
                };
            b = function(c) {
                var b = eval("a.temp." + c);
                b || (b = eval("h." + c));
                return g(b)
            };
            var n = b("boxes"),
                x = b("overlay"),
                e = b("inner"),
                p = b("title"),
                s = b("close"),
                u = b("content"),
                t = b("mess"),
                l = b("input"),
                m = b("button"),
                q = b("ok"),
                r = b("cancel"),
                d = {
                    el: {
                        boxes: n,
                        overlay: x,
                        inner: e,
                        title: p,
                        close: s,
                        content: u,
                        mess: t,
                        input: l,
                        button: m,
                        ok: q,
                        cancel: r
                    },
                    effect: function(a) {
                        "load" == a ? a = "" : "error" == a && (a = "");
                        x.css("background-image", "url(" + a + ")");
                        e.css({
                            opacity: 0,
                            top: "-100%"
                        })
                    },
                    removeEffect: function() {
                        0 == e.css("opacity") && (x.css("background-image", "none"), e.css("top", "-" + e.height()), e.animate({
                            opacity: 1,
                            top: "50%"
                        }))
                    },
                    curWidth: 0,
                    curHeight: 0,
                    center: function(b, h) {
                        h || (h = a.width);
                        e.width(h);
                        var c = e.width();
                        e.css({
                            width: c,
                            height: "auto",
                            "margin-left": "-" + c / 2 + "px"
                        });
                        var f = g(window).width(),
                            k = a.maxWidth;
                        k > f && (k = f);
                        c = c > k ? k : c;
                        f = a.minWidth;
                        c < f && (c = f);
                        e.css({
                            width: c,
                            "margin-left": "-" + (c / 2 + z[1]) + "px"
                        });
                        t && t.height("auto");
                        d.curWidth = c;
                        b || (b = a.height);
                        e.height(b);
                        c = 0;
                        m && (c = m.height());
                        c = e.height() + c;
                        e.css({
                            height: c,
                            "margin-top": "-" + c / 2 + "px"
                        });
                        f = g(window).height();
                        k = a.maxHeight;
                        k > f && (k = f);
                        c = c > k ? k : c;
                        f = a.minHeight;
                        c < f && (c = f);
                        e.css({
                            height: c,
                            "margin-top": "-" + (c / 2 + z[0]) + "px"
                        });
                        d.curHeight = c;
                        if (t) {
                            f = 0;
                            m && (f = m.outerHeight());
                            k = 0;
                            p && (k = p.outerHeight());
                            var n = 0;
                            l && (n = l.outerHeight(!0));
                            c -= k + n + f + 2 * A[0];
                            0 > c ? d.effect("error") : (t.height(c), d.removeEffect())
                        }
                    },
                    close: function() {
                        n && e.animate({
                            opacity: 0,
                            top: "-" + e.height()
                        }, 300, function() {
                            n.remove();
                            d.setHeight = d.setWidth = d.curHeight = d.curWidth = 0
                        })
                    }
                }, w = a.mode,
                C = a.title,
                h = a.message,
                D = a.inputTxt,
                A = a.padding,
                z = a.border,
                y = function() {
                    null === h ? h = "null" : "object" == g.type(h) && (h = "[object Object]");
                    a.messString && (h = h.toString().replace(/[<>\n\t]/g, function(a) {
                        return {
                            "<": "&lt;",
                            ">": "&gt;",
                            "\n": "<br />",
                            "\t": '<span style="display:inline-block;width:20px"></span>'
                        }[a]
                    }));
                    q && q.click(function() {
                        a.ok.call(q, d, a);
                        a.button(!0, d, a);
                        a.noClose || d.close()
                    });
                    r && r.click(function() {
                        a.cancel.call(r, d, a);
                        a.button(!1, d, a);
                        a.noClose || d.close()
                    });
                    s && s.click(function() {
                        d.noClose = !0;
                        a.close.call(s, d, a);
                        d.close()
                    });
                    if (l) l.on("input", function() {
                        a.input.call(l, d, a)
                    });
                    a.clickOut && x.click(function() {
                        d.close()
                    });
                    var b, y = function() {
                            b = setTimeout(function() {
                                d.close()
                            }, a.autoClose)
                        };
                    500 <= a.autoClose && (y(), e.hover(function() {
                        clearTimeout(b)
                    }, function() {
                        y()
                    }));
                    a.okBtnShow || /^alert|confirm|prompt$/.test(w) || (q = "");
                    a.cancelBtnShow || /^confirm|prompt$/.test(w) || (r = "");
                    q || r || (m = "");
                    a.inputTxtShow || "prompt" == w || (l = "");
                    !a.closeBtnShow && /^alert|confirm|prompt$/.test(w) && (s = "");
                    n.appendTo("body");
                    d.effect("load");
                    w && n.addClass(w);
                    n.append(x);
                    n.append(e);
                    e.css("border-width", z[0] + "px " + z[1] + "px");
                    e.append(u);
                    C && u.append(p.html(C));
                    u.append(t.html(h));
                    u.css({
                        padding: A[0] + "px " + A[1] + "px"
                    });
                    s && e.append(s.html(a.closeBtn));
                    l && (u.append(l), D && l.val(D));
                    m && (e.append(m), q && m.append(q.html(a.okBtn)), r && m.append(r.html(a.cancelBtn)), m.css({
                        padding: "0 " + A[1] + "px"
                    }));
                    var c = t.find("img"),
                        f = c.length,
                        k = function() {
                            d.center();
                            d.removeEffect()
                        };
                    if (f) {
                        var v = 0;
                        c.each(function(a, b) {
                            this.complete ? (v++, f == v && k()) : g(this).load(function() {
                                v++;
                                f == v && k()
                            }).error(function() {
                                v++;
                                f == v && (g(this).replaceWith('<span style="color:red">[ Image not loading: ' + this.src + " ]</span>"), k())
                            })
                        })
                    } else k(); if (a.autoResize) {
                        var B = !0;
                        g(window).on("resize", function() {
                            B && (setTimeout(function() {
                                d.center(height, width);
                                B = !0
                            }, 300), B = !1)
                        })
                    }
                    if (l) {
                        var c = l.get(0),
                            E = c.value.length;
                        c.selectionStart = 0;
                        c.selectionEnd = E;
                        c.focus()
                    }
                };
            void 0 === this.selector ? (y(), a.helper.call(document, d, a)) : this.click(function(b) {
                b.preventDefault();
                y();
                a.helper.call(this, d, a)
            })
        },
        alert: function(b, a) {
            g.boxes({
                mode: "alert",
                message: b,
                ok: function() {
                    "function" === g.type(a) && a.apply({
                        data: void 0
                    })
                }
            })
        },
        confirm: function(b, a) {
            g.boxes({
                mode: "confirm",
                message: b,
                button: function(b) {
                    "function" === g.type(a) && a.apply({
                        data: b
                    })
                }
            })
        },
        prompt: function(b, a, h) {
            g.boxes({
                mode: "prompt",
                message: b,
                inputTxt: a,
                button: function(a, b) {
                    if ("function" === g.type(h)) {
                        var e = null;
                        a && (e = b.el.input.val());
                        h.apply({
                            data: e
                        })
                    }
                }
            })
        }
    };
    g.fn.boxes = g.boxes = function(b) {
        if (p[b]) {
            var a = this,
                h = Array.prototype.slice.call(arguments, 1);
            void 0 === this.selector ? p[b].apply(a, h) : this.click(function(g) {
                g.preventDefault();
                p[b].apply(a, h)
            })
        } else if ("object" !== typeof b && b) window.console && console.error("Method " + b + " does not exist on jQuery.boxes");
        else return p.init.apply(this, arguments)
    }
})(jQuery);
